
MAC=$(ifconfig eth0 | grep 'ether ' | awk '{print $2}')
cat /etc/mosquitto/mosquitto.conf  | grep $MAC
if [ $? -eq 1 ];then
  sudo systemctl stop mosquitto.service
  sudo systemctl stop zigbee2mqtt.service
  echo "MAC changed, new device"
  sudo bash -c "sed  s/GW_MAC/$MAC/g  /home/pi/workspace/lepi-gui/conf/mosquitto.conf > /etc/mosquitto/mosquitto.conf"
  sudo bash -c "sed -i  s/MQ_ID/$MAC/g  /etc/mosquitto/mosquitto.conf"
  if [ -e /var/lib/mosquitto/mosquitto.db ];then
    echo "clear mosquitto db"
    sudo rm /var/lib/mosquitto/mosquitto.db
  fi
  if [ -e /opt/zigbee2mqtt/data/database.db ];then
    echo "clear zigbee2mqtt db"
    sudo rm /opt/zigbee2mqtt/data/database.db
  fi
  if [ -e /opt/zigbee2mqtt/data/coordinator_backup.json ];then
    echo "clear coordinator_backup.json"
    sudo rm /opt/zigbee2mqtt/data/coordinator_backup.json
  fi
  sudo cp /home/pi/workspace/lepi-gui/conf/zigbee2mqtt.yaml  /opt/zigbee2mqtt/data/configuration.yaml
  echo "enable and start service"
  sudo systemctl enable --now mosquitto.service
  sudo systemctl enable --now zigbee2mqtt.service
fi
